<template>
  <div class="playlist-container">
    <!-- 歌单信息 -->
    <div class="playlist-info">
      <!-- 歌单封面 -->
      <el-image style="width: 182px; height: 182px; border-radius: 6px" v-if="playListMsg.coverImgUrl !== undefined" :src="playListMsg.coverImgUrl" alt=""></el-image>
      <!-- 歌单名称 -->
      <div class="name-box">
        <span class="type">歌单</span>
        <h2>{{ playListMsg.name }}</h2>
      </div>
      <!-- 创建信息 -->
      <div class="create" v-if="playListMsg.creator">
        <!-- 用户头像 -->
        <img :src="playListMsg.creator.avatarUrl" alt="" />
        <!-- 用户名 -->
        <span>{{ playListMsg.creator.nickname }}</span>
        <!-- 创建时间 -->
        <i>{{ playListMsg.createTime | conversion('yyyy-MM-dd') }}创建</i>
      </div>
      <!-- 播放全部 -->
      <div class="play-all" @click="open">
        <i class="el-icon-caret-right"></i>
        <span>播放全部</span>
        <i class="el-icon-plus"></i>
      </div>
      <!-- 标签 -->
      <div class="tags">
        <i>标签：</i>
        <span v-for="(item, index) in playListMsg.tags" :key="index">{{ item }}</span>
      </div>
      <!-- 其他 -->
      <div class="other-msg">
        <i>歌曲 : {{ playListMsg.trackCount }}</i>
        <i>播放 : {{ playListMsg.playCount | change }}</i>
      </div>
      <!-- 简介 -->
      <div class="description">
        <p>
          <pre ref="preRef" style="height: 15px">简介: {{ playListMsg.description }}</pre>
        </p>
        <i ref="iconRef" class="el-icon-caret-bottom" @click="showDescription"></i>
      </div>
    </div>
    <!-- 歌曲列表 -->
    <div class="playlist-songlist">
      <el-table :data="allSongDetailList" stripe style="width: 100%">
        <el-table-column type="index" width="75" align="center"></el-table-column>
        <el-table-column prop="name" label="标题" min-width="40%">
          <template slot-scope="scope">
            <span style="display: inline-block; width: 100%" @click="chooseSong(scope.row.id)">{{ scope.row.name }}</span>
            <i class="el-icon-star-on" :class="isLoved(scope.row.id)" @click="myLove(scope.row)"></i>
          </template>
        </el-table-column>
        <el-table-column prop="ar[0].name" label="歌手" min-width="20%"></el-table-column>
        <el-table-column prop="al" label="专辑" min-width="25%">
          <template slot-scope="scope">
            <span style="cursor: pointer" @click="goAlbum(scope.row.al.id)">{{ scope.row.al.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="dt" label="时间" min-width="15%">
          <template slot-scope="scope">{{ scope.row.dt | conversion('mm : ss') }}</template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getPlayListByIdAPI } from '@/api/playListAPI.js'
import { getSongByIdAPI } from '@/api/Song.js'
import { savePlayList, clearPlayList,saveMyLoveSongList,loadMyLoveSongList,deleteMyLoveSongList } from '@/js/cache'

import bus from '@/components/eventBus.js'

export default {
  name: 'PlayList',
  data() {
    return {
      id: this.$route.query.id,
      // 歌单详情
      playListMsg: {},
      // 简介展开状态 默认不展开
      descriptionState: false,
      // 歌单所有歌曲详情
      allSongDetailList:[],
      myLoveSongList:[]
    }
  },
  created(){
    bus.$on('initLoveState',()=>{
      this.myLoveSongList = loadMyLoveSongList()
    })
  },
  mounted() {
    this.getPlayList()
    this.myLoveSongList = loadMyLoveSongList()
  },
  methods: {
    // 获取歌单详情
    async getPlayList() {
      const {
        data: { playlist: res }
      } = await getPlayListByIdAPI(this.id)
      this.playListMsg = res
      // 歌单所有歌曲
      res.trackIds.forEach(element => {
        // 循环调用获取歌曲详情方法
        this.getSong(element.id)
      });
    },
    showDescription(){
      if(!this.descriptionState) {
        // 没展开时
        this.$refs.preRef.style.height= '';
        this.$refs.iconRef.className='el-icon-caret-top';
      }else {
        // 展开时
        this.$refs.preRef.style.height= '15px';
        this.$refs.iconRef.className='el-icon-caret-bottom'
      }
      // 变更展开状态
      this.descriptionState = !this.descriptionState
    },
    // 获取歌曲详情
    async getSong(ids) {
      // 歌曲详情
      const {
        data: { songs: res }
      } = await getSongByIdAPI(ids)
      // 将歌曲详情存入列表
      this.allSongDetailList.push(res[0])
      // // 歌曲封面
      // this.picUrl = this.songDetail.al.picUrl
      // // 歌词
      // const {
      //   data: {
      //     lrc: { lyric: lyrStr }
      //   }
      // } = await getLyricByIdAPI(this.id)
      // this.lyric = lyrStr
      // // 初始化拆分歌词
      // this.lyrReady = this.formatLyric(this.lyric)
      // // 目前歌词，默认 0 秒时
      // this.nowLyr = this.lyrReady[0]
    },
    chooseSong(id){
      bus.$emit('songId',id)
    },
    goAlbum(id){
      this.$router.push({
        path: '/album',
        query: {
          id: id
        }
      })
    },
    open() {
      this.$confirm('"播放全部"将会替换当前的播放列表，是否继续？', '替换播放列表', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        lockScroll: false
      })
        .then(() => {
          // 执行替换当前的播放列表的操作
          // 清空当前播放列表
          clearPlayList()
          // 准备新的列表数据，存入播放列表中
          this.allSongDetailList.forEach((item) => {
            let obj = {
              id: item.id,
              name: item.name,
              artist: item.ar[0].name,
              time: item.dt
            }
            savePlayList(obj)
          })
          // 重新渲染播放列表数据
          bus.$emit('initPlayList')
        })
        .catch(() => {
          // console.log('取消')
        })
    },
    myLove(row) {
      // 判断是否存在
      const index = this.myLoveSongList.findIndex((item) => {
        return item.id === row.id
      })
      if (index >= 0) {
        // 已存在,删除
        deleteMyLoveSongList(row)
      } else {
        // 不存在，添加
        let obj = {
          id: row.id,
          name: row.name,
          artist: row.ar,
          album: row.al,
          time: row.dt
        }
        saveMyLoveSongList(obj)
      }
      this.myLoveSongList = loadMyLoveSongList()
      // 重新渲染 class
      bus.$emit('initLoveState')
    },
    // 判断歌曲是否已喜欢，动态添加 class
    isLoved(id) {
      const index = this.myLoveSongList.findIndex((item) => {
        return item.id === id
      })
      if (index >= 0) {
        return 'loved'
      }
    }
  },
  watch:{
        $route: {
      handler(newVal) {
        this.id = newVal.query.id
        this.getPlayList()
      },
      deep: true
    }
  }
}
</script>

<style lang="less" scoped>
.playlist-container {
  .playlist-info {
    .name-box {
      text-align: left;
      padding-top: 30px;
      .type {
        padding: 0 2px;
        font-size: 14px;
        color: #EC4141;
        border: 1px solid #EC4141;
        border-radius: 3px;
      }
      h2 {
        display: inline-block;
        margin-left: 10px;
        font-size: 20.5px;
      }
    }
    .el-image {
      float: left;
      margin: 30px;
    }
    .create {
      overflow: hidden;
      margin-top: 10px;
      img {
        float: left;
        width: 24.5px;
        height: 24.5px;
        border-radius: 12.25px;
      }
      span {
        float: left;
        margin-left: 10px;
        font-size: 12px;
        line-height: 24.5px;
        color: #0b58b0;
        opacity: 0.6;
      }
      i {
        float: left;
        margin-left: 10px;
        font-size: 12px;
        line-height: 24.5px;
        color: #676767;
      }
    }
    .play-all {
      position: relative;
      margin-top: 10px;
      margin-left: 242px;
      width: 140px;
      height: 32.5px;
      line-height: 32.5px;
      background-color: #ec4141;
      text-align: center;
      border-radius: 16.25px;
      cursor: pointer;
      &:hover {
        background-color: #cc3232;
      }
      .el-icon-caret-right {
        position: absolute;
        top: 50%;
        margin-top: -12px;
        left: 10px;
        font-size: 24px;
        color: #fff;
      }
      span {
        margin: 0;
        font-size: 17px;
        color: #fff;
      }
      .el-icon-plus {
        position: absolute;
        top: 50%;
        margin-top: -8px;
        right: 10px;
        font-size: 16px;
        color: #fef4f4;
      }
    }
    .tags {
      overflow: hidden;
      margin-top: 10px;
      i {
        float: left;
        font-size: 14px;
      }
      span {
        float: left;
        margin: 0 5px;
        padding: 0 5px;
        font-size: 14px;
        background-color: rgba(224, 224, 224, 0.8);
        border-radius: 999px;
      }
    }
    .other-msg {
      overflow: hidden;
      margin-top: 10px;
      i {
        float: left;
        margin-right: 10px;
        font-size: 14px;
      }
    }
    .description {
      position: relative;
      overflow: hidden;
      margin-top: 10px;
      font-size: 14px;
      text-align: left;
      pre {
        width: 1025px;
        white-space: pre-wrap;
        word-wrap: break-word;
      }
      .el-icon-caret-bottom,
      .el-icon-caret-top {
        position: absolute;
        top: 0;
        right: 30px;
        color: #999999;
        font-size: 18px;
        cursor: pointer;
      }
    }
  }
  // 歌曲列表
  .playlist-songlist {
    /deep/.el-table__cell {
      padding: 9px 0;
      font-size: 12px;
    }
    .el-icon-star-on {
      position: absolute;
      top: 50%;
      margin-top: -10px;
      left: -68px;
      font-size: 20px;
      color: #e5e5e5;
      cursor: pointer;
    }
    .loved {
      color: #ec4141;
    }
  }
}
</style>
